package com.tanguy.user.base;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.ColorInt;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.PopupWindow;

import com.tanguy.user.R;
import com.tanguy.user.interfaces.RequestHelper;
import com.tanguy.user.utlis.ToastUtils;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import cn.sinata.xldutils_kotlin.widget.ProgressDialog;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

/**
 * Created by dab on 2017/9/29 0029 10:39
 * 弹窗的父类
 */

public abstract class BasePopupWindow extends PopupWindow implements RequestHelper {
    private static final String TAG = "BasePopupWindow";
    private Window mWindow;
    private Context mContext;
    private ProgressDialog mProgressDialog;
    private CompositeDisposable mCompositeDisposable;

    @Override
    public void cancelRequest(@NotNull Disposable disposable) {
        if (mCompositeDisposable == null) mCompositeDisposable = new CompositeDisposable();
        mCompositeDisposable.add(disposable);
    }

    @Override
    public void showToast(@Nullable String msg) {
        ToastUtils.showToast(msg);
    }

    @Override
    public void showLoadDialog(@NotNull String msg, boolean canCancel) {
        if (mProgressDialog == null)
            mProgressDialog = new ProgressDialog(mContext, R.style.Theme_ProgressDialog);
        if (!isShowing()) return;
        mProgressDialog.setCanceledOnTouchOutside(false);
        mProgressDialog.setMessage(msg);
        if (!mProgressDialog.isShowing()) {
            mProgressDialog.show();
        }
    }


    @Override
    public void dismissLoadDialog() {
        if (mProgressDialog != null) {
            mProgressDialog.dismiss();
        }
    }

    @LayoutRes
    public abstract int setContentRes();

    @ColorInt
    public int setBackgroundColor() {
        return Color.TRANSPARENT;
    }

    public float setBackgroundAlpha() {
        return 0.7f;
    }

    public boolean setFocusable() {
        return true;
    }

    public void initView(@NonNull View view) {
    }

    public void initEvent() {

    }

    public BasePopupWindow(Context context) {
        super(context);
        mContext = context;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//        if (inflater == null) return;
        View menuView = inflater.inflate(setContentRes(), null);
//        initView(menuView);
        //设置SelectPicPopupWindow的View
        this.setContentView(menuView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
//        this.setHeight(AutoUtils.scaleValue(1841));
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(setFocusable());
        //设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.AnimBottomToTop);
        //实例化一个ColorDrawable颜色为全透明
//        ColorDrawable dw = new ColorDrawable(Constant.COLOR_CONFIRM_DELETE_POPUP_WINDOW);
        ColorDrawable dw = new ColorDrawable(setBackgroundColor());
//        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
    }

    protected static void changeWindowAlpha(Window window, float alpha) {
        if (window == null) {
            Log.e(TAG, "changeWindowAlpha: window == null");
            return;
        }

        WindowManager.LayoutParams lp = window.getAttributes();
        lp.alpha = alpha;
        window.setAttributes(lp);

    }


    public void showAtLocation(Activity activity, View parent, int gravity, int x, int y) {
        super.showAtLocation(parent, gravity, x, y);
        //隐藏软键盘
        mWindow = activity.getWindow();
        mWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            View currentFocus = activity.getCurrentFocus();
            if (currentFocus != null) {
                imm.hideSoftInputFromWindow(currentFocus.getWindowToken(), 0); //强制隐藏键盘}
            }
        }
        changeWindowAlpha(mWindow, setBackgroundAlpha());
        initView(getContentView());
        initEvent();
    }

    @Override
    public void dismiss() {
        super.dismiss();
        changeWindowAlpha(mWindow, 1f);
        mWindow = null;
        mContext = null;
        if (mCompositeDisposable != null && !mCompositeDisposable.isDisposed()) {
            mCompositeDisposable.dispose();
        }
    }
}
